package com.iflysse.bbs.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.iflysse.bbs.dao.NewsDao;
import com.iflysse.bbs.model.NewsInfo;
import com.iflysse.bbs.util.JdbcUtil;

public class NewsDaoImpl implements NewsDao {
	private QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());

	@Override
	public boolean add(NewsInfo news) {
		boolean flag = false;
		String sql = "insert into news(title,content,date,userid) values(?,?,?,?)";
		Object[] params = { news.getTitle(), news.getContent(), news.getDate(),
				news.getUserId() };
		try {
			int row = runner.update(sql, params);
			flag = row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean edit(NewsInfo news) {
		boolean flag = false;
		String sql = "update news set title=?,content=?,date=?,userid=? where id=?";
		Object[] params = { news.getTitle(), news.getContent(), news.getDate(),
				news.getUserId(), news.getId() };
		try {
			int row = runner.update(sql, params);
			flag = row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public boolean deleteById(int id) {
		boolean flag = false;
		String sql = "delete from news where id=?";
		try {
			int row = runner.update(sql, id);
			flag = row > 0 ? true : false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public NewsInfo getNewsInfo(int id) {
		NewsInfo news = null;
		String sql = "select news.*,user.name as userName from news left join user on news.userid=user.id where news.id=?";
		try {
			news = runner.query(sql, new BeanHandler<NewsInfo>(NewsInfo.class),
					id);
		} catch (Exception e) {
			e.printStackTrace();			
		}
		return news;
	}

	@Override
	public List<NewsInfo> getNewsList() {
		List<NewsInfo> list = null;
		String sql = "select news.*,user.name as userName from news left join user on news.userid=user.id";
		try {
			list = runner.query(sql, new BeanListHandler<NewsInfo>(
					NewsInfo.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<NewsInfo> getNewsList(String title) {
		List<NewsInfo> list = null;
		String sql = "select news.*,user.name as userName from news left join user on news.userid=user.id where news.title like ?";
		try {
			list = runner.query(sql, new BeanListHandler<NewsInfo>(
					NewsInfo.class), "%" + title + "%");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<NewsInfo> getNewsList(int userId) {
		List<NewsInfo> list = null;
		String sql = "select news.*,user.name as userName from news left join user on news.userid=user.id where news.userid=?";
		try {
			list = runner.query(sql, new BeanListHandler<NewsInfo>(
					NewsInfo.class), userId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public List<NewsInfo> getNewsList(String title, int userId) {
		List<NewsInfo> list = null;
		String sql = "select news.*,user.name as userName from news left join user on news.userid=user.id where news.userid=? and news.title like ?";
		Object[] params = { userId, "%" + title + "%" };
		try {
			list = runner.query(sql, new BeanListHandler<NewsInfo>(
					NewsInfo.class), params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

}
